package com.party.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.party.domain.Discuss;
import com.party.domain.User;
import com.party.dto.Page;
import com.party.dto.Response;
import com.party.service.DiscussService;

/**
 * 
 * @author Caizhf
 * @date 2017年5月14日下午3:12:09
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: 评论</p>
 *
 */
@RestController
@RequestMapping("/discuss")
public class DiscussController {
	@Autowired
	private DiscussService discussService;

	/**
	 * 评论
	 * 传入评论的商品
	 * @param discuss
	 * @return
	 */
	/*@RequestMapping("/add")
	public Response add(Discuss discuss){
		Discuss d = discussService.evaluate(discuss);
		return Response.success(d);
	}*/
	@RequestMapping("/add")
	public Response add(Discuss discuss){
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		discuss.setUser(user);
		Discuss d = discussService.evaluate(discuss);
		return Response.success(d);
	}
	
	
	@RequestMapping("/delete/{did}")
	public Response delete(@PathVariable int did){
		discussService.deleteEvaluate(did);
		return Response.success("删除成功");
	}
	
	@RequestMapping("/update")
	public Response update(Discuss discuss){
		Discuss d = discussService.updateEvaluate(discuss);
		return Response.success(d);
	}
	
	//路径：http://127.0.0.1:8080/party/discuss/get?did=1
	@RequestMapping("/get")
	public Response get(@RequestParam("did") int did){
		Discuss d = discussService.find(did);
		return Response.success(d);
	}
	
	/**
	 * 查看用户所有的评价（无论什么商品）
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/get/{user_id}")
	public Response getUserAllEvaluate(@PathVariable int user_id){
		List<Discuss> list = discussService.findUserAllEvaluate(user_id);
		return Response.success(list);
	}
	
	/**
	 * 查看某个商品下所有的评价，分页显示
	 * @param product_id
	 * @param pageNum 页码
	 * @return
	 */
	@RequestMapping("/get/{product_id}/{pageNum}")
	public Response getUserAllEvaluate(@PathVariable int product_id,@PathVariable int pageNum){
		Page page = discussService.findProductAllEvaluate(product_id, pageNum);
		return Response.success(page);
	}
}
